package com.m3839.sdk.common.util;

import android.text.TextUtils;
import android.util.Log;

/**
 * Description:LogUtils
 *
 * @author 陈宝阳
 * @create 2021/6/7 14: 44
 */
public class LogUtils {
  private static final String PREFIX_TAG = "Hykb_";
  public static final String DEFAULT_TAG = "M3839SDK";
  public static final String LOG_MSG_PREFIX = "log start --> ";
  public static final String LOG_MSG_SUFFIX = "<-- log end ";
  public static final String LOG_SEPARATOR = " - ";
  /** java文件后缀 */
  private static final String JAVA_FILE_SUFFIX = ".java";
  /** kotlin文件后缀 */
  private static final String KOTLIN_FILE_SUFFIX = ".java";

  private static boolean isDebug = false;

  public static void setDebug(boolean debug) {
    isDebug = debug;
  }

  public static void i(String tag, String msg) {
    if (isDebug && !TextUtils.isEmpty(msg)) {
      Log.i(PREFIX_TAG + tag, msg);
    }
  }

  public static void d(String tag, String msg) {
    if (isDebug && !TextUtils.isEmpty(msg)) {
      Log.d(PREFIX_TAG + tag, msg);
    }
  }

  public static void e(String tag, String msg) {
    if (isDebug && !TextUtils.isEmpty(msg)) {
      Log.i(PREFIX_TAG + tag, msg);
    }
  }

  /**
   * 强制性的日志输出，用于在游戏接入，查看排查
   */
  public static void flog(String... messages){
    StringBuilder msgBuilder = new StringBuilder(LOG_MSG_PREFIX);
    for (int i = 0; i < messages.length; i++) {
      msgBuilder.append(messages[i]);
      if(i == messages.length - 1) {
        break;
      }
      msgBuilder.append(LOG_SEPARATOR);

    }
    msgBuilder.append(LOG_MSG_SUFFIX);
    //处理log
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    String tag = DEFAULT_TAG;
    if (stackTrace.length > 4) {
      StackTraceElement log = stackTrace[3];
      tag = log.getFileName();
      if (tag.contains(JAVA_FILE_SUFFIX)) {
        tag = tag.replace(JAVA_FILE_SUFFIX, "");
      } else if (tag.contains(KOTLIN_FILE_SUFFIX)) {
        tag = tag.replace(KOTLIN_FILE_SUFFIX, "");
      }
    }
    i(tag, msgBuilder.toString());
  }


}
